﻿

using System;
using System.Collections;
using System.IO;
using System.Net.Http;
using System.Text;
using System.Web;
using System.Web.Http;
using System.Web.Http.Controllers;
using System.Web.Http.Filters;
using TY.UC.Common;
using TY.UC.Common.Extension;
using TY.UC.Http;
using TY.UT.Common.Http;

namespace TY.UI.WebApi
{
    /// <summary>
    /// Api签名验证过滤器
    /// </summary>
    public class ApiAuthorityAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(HttpActionContext actionContext)
        {
            string intstr;
            var httpContext = HttpContext.Current;
            httpContext.Request.InputStream.Position = 0;
            StreamReader st = new StreamReader(httpContext.Request.InputStream, Encoding.UTF8);
            {
                intstr = st.ReadToEnd();
            }
            httpContext.Request.InputStream.Position = 0;
            if (!string.IsNullOrEmpty(intstr))
            {
                //记录接口请求参数
                LogHelper.WriteLog("AuthorFilter访问地址：" + httpContext.Request.Url + "，参数：" + intstr);
                TyRequestModel m;
                TyResultModel r;
                try
                {
                    m = intstr.JsonDe<TyRequestModel>();
                    if (m == null)
                    {
                        r = new TyResultModel
                        {
                            code = "-1",
                            msg = "参数解析失败",
                            data = ""
                        };
                        actionContext.Response = new HttpResponseMessage { Content = new StringContent(r.JsonSe(), Encoding.GetEncoding("UTF-8"), "application/json") };
                        return;
                    }
                }
                catch (Exception ex)
                {
                    r = new TyResultModel
                    {
                        code = "-3",
                        msg = "参数解析失败",
                        data = ""
                    };
                    LogHelper.WriteLog("ApiAuthorityAttribute-OnActionExecuting,入参：" + actionContext.JsonSe(), ex);
                    actionContext.Response = new HttpResponseMessage { Content = new StringContent(r.JsonSe(), Encoding.GetEncoding("UTF-8"), "application/json") };
                    return;
                } 
                //服务器签名
                var serverSign = ParamEncryption.GetParamSign(m);
                if (serverSign.ToUpper() == m.Sign.ToUpper())
                {
                    return;
                }
                r = new TyResultModel
                {
                    code = "-2",
                    msg = "认证失败",
                    data = ""
                };
                actionContext.Response = new HttpResponseMessage { Content = new StringContent(r.JsonSe(), Encoding.GetEncoding("UTF-8"), "application/json") };
            }
            else
            {
                TyResultModel r = new TyResultModel
                {
                    code = "-4",
                    msg = "参数解析失败",
                    data = ""
                };
                actionContext.Response = new HttpResponseMessage { Content = new StringContent(r.JsonSe(), Encoding.GetEncoding("UTF-8"), "application/json") };
            }
        }

    }
}